<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  fan.php
//  phpweb
//  
//  Created by WillLee on 2011-07-06.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Talk extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
 		$this->load->model('common/security_model');
		if($this->security_model->isLogin() === FALSE){
			showmessage('请登录', 'login', 2);
		}
		
		$this->load->model('user_model');
		$this->load->model('talk_model');
		$this->load->model('ship_model');
		$this->load->model('comment_model');
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
		$this->uid = $this->uri->segment(3) ? intval($this->uri->segment(3)) : $this->user['uid'];

	}
	
	public function index()
	{
		$data['userinfo'] = $this->user_model->get_user_smart($this->uid);
		$data['self'] = $this->security_model->checkself($this->uid);
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($this->uid);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		$data['talks'] = $this->talk_model->get_pagetalks($this->uid);
		$this->load->template('talk_tpl', $data);
	}
	
	
	public function detail(){
		$uid = $this->input->get('uid') ? intval($this->input->get('uid', true)) : '';
		$talkid = $this->input->get('talkid') ? intval($this->input->get('talkid', true)) : '';
		if (!$talkid) {
			showmessage('您访问的内容不存在', 'index', 2);
		}
		if (!$uid) {
			showmessage('您访问的用户不存在', 'index', 2);
		}
		
		$data['talk'] = $this->talk_model->get_talk('talkid', $talkid);
		$data['self'] = $this->security_model->checkself($uid);
		$data['comments'] = $this->comment_model->get_page_comments($talkid, 'talkid');
		$data['id'] = $talkid;
		$data['idtype'] = 'talkid';
		$data['comment_key'] = md5($talkid.'talkid'.$this->config->item('encryption_key'));
		$data['uid'] = $this->user['uid'];
		
		$this->load->template('talk_detail_tpl', $data);
	}
	
	// 删除我说
	public function remove($talkid){
		$talkid = intval($talkid);
		if (!$talkid) {
			showmessage('您访问的作品不存在', 'index', 2);
		}
		
		$data['talk'] = $this->talk_model->get_talk('talkid', $talkid);

		if ($this->user['uid'] != $data['talk']['uid']) {
			showmessage('不是您的我说，不能删除。', 'work/index/'.$wid, 2);
		}
		
		$data['userinfo'] = $this->user_model->getuser('uid', $this->user['uid']);
		
		$data['self'] = $this->security_model->checkself($this->user['uid']);
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($data['work']['uid']);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		
		$res = $this->talk_model->remove($talkid);
		if ($res) {
			// 更新session
			$this->security_model->update_user_session();
			redirect(base_url().'talk/index/', 'refresh');
		}else{
			showmessage('删除失败,请联系管理员。', 'talk/index/'.$this->user['uid'], 2);
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */